#!/bin/bash

# this is executed in the docker fpm image
set -e
cd /build

# add SNAPSHOT if it was requested
VERSION={{.version}}
if [ "$SNAPSHOT" = "yes" ]; then
    VERSION="${VERSION}-SNAPSHOT"
fi

mkdir /{{.beat_name}}-${VERSION}-linux-{{.bin_arch}}
cp -a homedir/. /{{.beat_name}}-${VERSION}-linux-{{.bin_arch}}/
cp {{.beat_name}}-linux-{{.arch}} /{{.beat_name}}-${VERSION}-linux-{{.bin_arch}}/{{.beat_name}}
cp {{.beat_name}}-linux.yml /{{.beat_name}}-${VERSION}-linux-{{.bin_arch}}/{{.beat_name}}.yml
cp {{.beat_name}}-linux.reference.yml /{{.beat_name}}-${VERSION}-linux-{{.bin_arch}}/{{.beat_name}}.reference.yml
cp fields.yml /{{.beat_name}}-${VERSION}-linux-{{.bin_arch}}/
cp -a modules.d-linux/ /{{.beat_name}}-${VERSION}-linux-{{.bin_arch}}/modules.d || true

mkdir -p upload
tar czvf upload/{{.beat_name}}-${VERSION}-linux-{{.bin_arch}}.tar.gz /{{.beat_name}}-${VERSION}-linux-{{.bin_arch}}
echo "Created upload/{{.beat_name}}-${VERSION}-linux-{{.bin_arch}}.tar.gz"

cd upload
sha1sum {{.beat_name}}-${VERSION}-linux-{{.bin_arch}}.tar.gz | awk '{print $1;}' > {{.beat_name}}-${VERSION}-linux-{{.bin_arch}}.tar.gz.sha1
echo "Created upload/{{.beat_name}}-${VERSION}-linux-{{.bin_arch}}.tar.gz.sha1"
